package org.com.yz.dubbo.service.impl;

import cn.hutool.core.date.DateTime;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.com.yz.common.constant.PayConstant;
import org.com.yz.common.util.MyLog;
import org.com.yz.common.util.MySeq;
import org.com.yz.common.util.ToolUtil;
import org.com.yz.dal.dao.mapper.DepositTransactionMapper;
import org.com.yz.dal.dao.mapper.InterfaceConfigMapper;
import org.com.yz.dal.dao.mapper.MchSubMapper;
import org.com.yz.dal.dao.model.DepositTransaction;
import org.com.yz.dal.dao.model.InterfaceConfig;
import org.com.yz.dal.dao.model.MchAccnt;
import org.com.yz.dal.dao.model.MchSubInfo;
import org.com.yz.dubbo.api.service.ICreateMchSubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.tools.Tool;
import java.util.UUID;

/**
 * 开设子商户接口实现类
 */
@Service(version = "1.0.0", interfaceName = "org.com.yz.dubbo.api.service.ICreateMchSubService", retries = -1)
@Transactional(rollbackFor = Exception.class)
public class CreateMchSubServiceImpl implements ICreateMchSubService {

    private static final MyLog _log = MyLog.getLog(CreateMchSubServiceImpl.class);

    @Autowired
    private MchSubMapper mchSubMapper;
    @Autowired
    private DepositTransactionMapper depositTransactionMapper;
    @Autowired
    private InterfaceConfigMapper interfaceConfigMapper;


    /**
     * 创建子商户账号
     *
     * @param parmJson
     * @return
     */
    @Override
    public MchAccnt createMchSub(String parmJson) {
        String result = StringEscapeUtils.unescapeJava(parmJson);
        MchSubInfo req = JSONObject.parseObject(result, MchSubInfo.class);
        MchAccnt accnt = req.getBizContent();
        MchAccnt accnt1 = new MchAccnt();
        DepositTransaction des = new DepositTransaction();
        //生成子商户账户
        String mchAccntNo = MySeq.getTrans();//子商户账号
        //更新账户表
        accnt.setMchAccntNo(mchAccntNo);//子商户账号
        accnt.setMchNo(req.getMchNo());//商户号
        accnt.setSettleDuration("T1");//结算周期
        accnt.setAccntType(PayConstant.DEPOSIT_MCHSUB);//后面应改为字典项
        if(ToolUtil.isNotEmpty(accnt.getIsSupplier()) && "Y".equals(accnt.getIsSupplier())){
            accnt.setAccntType(PayConstant.DEPOSIT_SUPPLIER);//后面应改为字典项
        }
        accnt.setRemainAmt(0L);
        accnt.setCreateTime(new DateTime());//创建时间
        mchSubMapper.insert(accnt);
        if(ToolUtil.isNotEmpty(accnt.getIsSupplier()) && "Y".equals(accnt.getIsSupplier())){
            accnt.setAccntType(PayConstant.DEPOSIT_SUPPLIER_ASSURANCE);//账户类型
            accnt.setMchAccntNo(MySeq.getTrans());//子商户账号
            accnt.setMchNo(req.getMchNo());//商户号
            accnt.setSettleDuration("T1");//结算周期
            accnt.setMchAccntName(accnt.getMchAccntName()+"(待结算)");//账户名称
            accnt.setRemainAmt(0L);//账户余额
            accnt.setCreateTime(new DateTime());//创建时间
            mchSubMapper.insert(accnt);
        }
        des.setTransactionNo(UUID.randomUUID().toString().replace("-", "").toLowerCase());//事务号
        des.setOutTransNo(req.getOutTransNo());//外部追踪号
        des.setIdMchAccntChargeUp(0);
        des.setCreateTime(new DateTime());//创建时间
        des.setTransactionType(PayConstant.MCHSUB_CREATE);//事务类型
        des.setUpdateTime(new DateTime());//更新时间
        des.setMchNo(req.getMchNo());//商户号
        //插入事务表
        depositTransactionMapper.insert(des);
        accnt1.setMchAccntNo(mchAccntNo);//子商户号
        accnt1.setOutMchAccntNo(accnt.getOutMchAccntNo());//外部子商户号
        return accnt1;//返回业务参数
    }

    /**
     * 查询token
     *
     * @param config
     * @return
     */
    @Override
    public InterfaceConfig selectSign(String config) {
        InterfaceConfig ifc = new InterfaceConfig();
        ifc.setMchNo(config);
        return interfaceConfigMapper.select(ifc).get(0);
    }
}
